package com.cooldatasoft.web.cms.node;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.cooldatasoft.app.WOCApplication;
import com.cooldatasoft.app.WOCSession;
import com.cooldatasoft.base.BaseViewPage;
import com.cooldatasoft.data.ComboValue;
import com.cooldatasoft.data.Node;
import com.cooldatasoft.data.Person;
import com.cooldatasoft.data.PersonPropertyValue;

public class NodeViewPage extends BaseViewPage{

	//FIXME bu element sayfada yok
	private FeedbackPanel feedbackPanel;

	public NodeViewPage(Integer entityId) {
		
		MarkupContainer container= new WebMarkupContainer("container");		
		MarkupContainer header = new WebMarkupContainer("header");
		ExternalLink companyWebsiteLink = new ExternalLink("companyWebsiteLink","http://www.cooldatasoft.com","Company Name");
		companyWebsiteLink.add(new AttributeModifier("target",true,new Model("_blank")));
		header.add(companyWebsiteLink);
		container.add(header);
		
		
		MarkupContainer wrapper = new WebMarkupContainer("wrapper");
		MarkupContainer personalInfoDiv = new WebMarkupContainer("personalInfoDiv");
		PersonPropertyForm personalInfoForm = new PersonPropertyForm("personalInfoForm",entityId);
		personalInfoDiv.add(personalInfoForm);
		wrapper.add(personalInfoDiv);
		
		container.add(wrapper);
		
		add(container);
	}

	



	class PersonPropertyForm extends Form{

		private Node node;
		private ComboValue managerComboValue;
		private ComboValue parentNodeComboValue;
		
		public PersonPropertyForm(String id,Integer entityId) {
			super(id);
			if(entityId!=null){
				node = WOCApplication.get().getNodeServiceProxy().get(entityId);
				
				Person managerPerson = WOCApplication.get().getPersonServiceProxy().get(node.getManagerId());
				PersonPropertyValue name =  WOCApplication.get().getPersonServiceProxy().getPersonPropertyValueById(managerPerson.getId(), 1);
				PersonPropertyValue surname =  WOCApplication.get().getPersonServiceProxy().getPersonPropertyValueById(managerPerson.getId(), 2);
				if(name != null && surname != null){
					managerComboValue = new ComboValue(managerPerson.getId(),name.getValue()+" "+surname.getValue());
				}
				
				Node  parentNodeTmp = WOCApplication.get().getNodeServiceProxy().get(node.getParentId());
				if(parentNodeTmp!=null){
					parentNodeComboValue = new ComboValue(parentNodeTmp.getId(),parentNodeTmp.getLabel());
				}
			}
			
			Label idLabel = new Label("idLabel","Id");
			Label nodeTypeLabel = new Label("nodeTypeLabel","Node Type");
			Label labelLabel = new Label("labelLabel","Label");
			Label managerLabel = new Label("managerLabel","Manager");
			Label parentLabel = new Label("parentLabel","Parent");
			
			add(idLabel);
			add(nodeTypeLabel);
			add(labelLabel);
			add(managerLabel);
			add(parentLabel);
			
			
			Label idValue = new Label("idValue",new PropertyModel(this,"node.id"));
			
			
			List<String> dataTypeList = new ArrayList<String>();
			dataTypeList.add("STRING");
			DropDownChoice dataTypeDropDownChoice = 
				new DropDownChoice("nodeTypeDropDownChoice",new PropertyModel(this,"node.nodeType"),dataTypeList);
			
			
			TextField labelTextField = new TextField("labelTextField",new PropertyModel(this,"node.label"));
			
			
//			TextField parentTextField = new TextField("parentTextField",new PropertyModel(this,"parentId"));
		
			
			List<ComboValue> managerOptionlist = WOCApplication.get().getPersonServiceProxy().getPersonSelectOptions();
			ChoiceRenderer choiceRenderer = new ChoiceRenderer("value", "key");
			DropDownChoice managerDropDown = 
				new DropDownChoice("managerDropDown", new PropertyModel(this, "managerComboValue"), managerOptionlist, choiceRenderer);
			
			List<ComboValue> nodeOptionlist = WOCApplication.get().getNodeServiceProxy().getNodeSelectOptions();
			DropDownChoice parentDropDown = 
				new DropDownChoice("parentNodeDropDown", new PropertyModel(this, "parentNodeComboValue"), nodeOptionlist, choiceRenderer);
			
			
			add(managerDropDown);
			add(parentDropDown);
			add(idValue);
			add(dataTypeDropDownChoice);
			add(labelTextField);					
			Button personalInfoSaveButton = new Button("saveButton");
			add(personalInfoSaveButton);			
		}

		@Override
		protected void onSubmit() {
			getNode().setUpdateDate(new Date());
			getNode().setCreateUserId(WOCSession.get().getUser().getId());
			getNode().setUpdateUserId(WOCSession.get().getUser().getId());
			getNode().setManagerId(getManagerComboValue().getKey());
			getNode().setParentId(getParentNodeComboValue().getKey());
			
			WOCApplication.get().getNodeServiceProxy().saveOrUpdate(getNode());
			
			setResponsePage(new NodeListPage());
		}

		public Node getNode() {
			return node;
		}

		public void setNode(Node node) {
			this.node = node;
		}

		public ComboValue getManagerComboValue() {
			return managerComboValue;
		}

		public void setManagerComboValue(ComboValue managerComboValue) {
			this.managerComboValue = managerComboValue;
		}

		public ComboValue getParentNodeComboValue() {
			return parentNodeComboValue;
		}

		public void setParentNodeComboValue(ComboValue parentNodeComboValue) {
			this.parentNodeComboValue = parentNodeComboValue;
		}



		
	}
}
